<template>
  <div>
    <Searchbar>
      <el-input @change="getList" v-model="search.id" placeholder="请输入镜像ID"></el-input>
      <el-input style="margin-right: 10px" @change="getList" v-model="search.experiment_name" placeholder="请输入实验名称"></el-input>

<!--      <el-select @change="onImage_typeChange" v-model="search.image_type" placeholder="请选择镜像格式">-->
<!--        <el-option-->
<!--          v-for="item in image_types"-->
<!--          :key="item.value"-->
<!--          :label="item.label"-->
<!--          :value="item.value"-->
<!--        ></el-option>-->
<!--      </el-select>-->

      <el-button @click="getList" type="primary">查询</el-button>
      <el-button  class="search-btn" @click="onShowCreate" type="primary" icon="el-icon-plus">新建</el-button>
       <el-button class="search-btn" @click="asyncImage" type="primary" icon="el-icon-refresh">同步</el-button>
    </Searchbar>

<!--    <TableBar>-->
<!--      <el-button @click="onShowCreate" type="primary" icon="el-icon-plus">新建</el-button>-->
<!--       <el-button @click="asyncImage" type="primary" icon="el-icon-refresh">同步</el-button>-->
<!--    </TableBar>-->

    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed="left" type="index" label="序号" width="55"></el-table-column>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="image_name" label="镜像名称"></el-table-column>
      <!-- <el-table-column prop="image_type" label="镜像类型"></el-table-column> -->
      <el-table-column prop="opration_system" label="操作系统"></el-table-column>
      <el-table-column prop="version" label="系统版本"></el-table-column>
      <el-table-column prop="import_status_label" label="导入状态"></el-table-column>
<!--      <el-table-column label="导入信息">-->
<!--        <template slot-scope="scope">-->
<!--          <div v-if="scope.row.import_status == 3">-->
<!--          导入失败，请参阅<a href="https://cloud.tencent.com/document/product/213/4945#%E5%AF%BC%E5%85%A5%E5%87%86%E5%A4%87">导入镜像准备</a>-->
<!--          </div>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="create_time" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- <el-button v-if="scope.row.import_status == 0" type="text" @click="onImport(scope.row)" size="small" class="text-default">导入</el-button> -->
          <!-- <el-button v-if="scope.row.import_status == 1" type="text" @click="onDescImport(scope.row)" size="small" class="text-default">查询导入状态</el-button> -->
          <!-- <el-button @click="onEdit(scope.row)" size="small" class="text-default">修改</el-button> -->
          <el-button @click="onDel(scope.row)" size="small" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
  </div>
</template>
<script>
import pageMixin from "@/mixins/pageMixin";
import { imageModel } from "@/api/models";
import Searchbar from "@/components/Searchbar";
import TableBar from "@/components/TableBar";
import ImageTypes from '@/common/image_types.js';
import {Message} from 'element-ui';
import ImageSelects from '@/common/image_selects.js';
export default {
  mixins: [pageMixin],
  components: { Searchbar, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        id: null,
        experiment_name: null,
        image_type: null,
        page: 1,
        page_size: 10
      },
      image_types: ImageTypes.types,
      import_status: ImageSelects.import_status
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    onShowCreate() {
      this.$router.push('/image/import');
    },
    onEdit(row) {
      this.$router.push('/image/modify1/' + row.id);
    },
    onDescImport(row) {
      imageModel.describe({
        id: row.id
      }, res => {
        Message.success(res.message)
        this.getList()
      }, err => {
        console.log(err)
      })
    },
    onImport(row) {
      imageModel.import({
        id: row.id
      }, res => {
        Message.success(res.message)
        this.getList()
      }, err => {
        this.getList()
        console.log(err)
      })
    },
    onDel(row) {
      this.$confirm('确定删除镜像' , '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        imageModel.batchRem({
          images: [row.id]
        }, res => {
          Message.success(res.message);
          this.getList();
        }, err => {
          Message.success(err.message);
        })
      })
    },

    asyncImage() {
      this.$confirm('确定同步所有镜像？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        imageModel.AsyncImage({

        }, res => {
          Message.success(res.message);
          this.getList();
        }, err => {
          Message.error(err.message);
        })
      })
    },


    onImage_typeChange() {
      this.getList();
    },
    getList() {
      console.log("getList");
      imageModel.list(
        this.search,
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            v.import_status_label = this.importStatusToLabel(v.import_status)
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    },
    importStatusToLabel(type) {
      let label;
      this.import_status.map(v => {
        if (v.value == type) label = v.label;
      });
      return label;
    }

  }
};
</script>
<style lang="scss">
</style>